import pygtk
pygtk.require('2.0')
import gtk
import gobject
import threading, time
from sys import exit, exc_info
import read_config

class zabbix_menu():
	
	configuration = None
	
	def __init__(self, configuration):
		self.configuration = configuration
		self.statusIcon = gtk.StatusIcon()
		self.statusIcon.set_visible(False)
		self.statusIcon.set_tooltip("Zabbix Notify")

		self.menu = gtk.Menu()

		self.handlerid = self.statusIcon.connect('activate', self.popup_menu_cb, self.menu)
		self.statusIcon.set_from_file(self.configuration.get_icon_off())

	# def popup_menu_cb(self, widget, button = None, data = None):
	def popup_menu_cb(self, widget, menu, data = None):
		menu.show_all()
		menu.popup(None, None, gtk.status_icon_position_menu,
			1, 0, widget)
	
	def set_visible(self, value):
		self.statusIcon.set_visible(value)

	def add_item(self, item):
		self.menu.append(gtk.MenuItem(item))
		
	def destroy(self):
		# First disconnect menu handler from tray icon
		self.statusIcon.disconnect(self.handlerid)
		# Delete entire menu...
		self.menu.destroy()
		# And "delete" statusIcon
		self.statusIcon.set_visible(False)
		self.statusIcon = None
		
	def empty(self):
		# First disconnect menu handler from tray icon
		self.statusIcon.disconnect(self.handlerid)
		# Delete entire menu...
		self.menu.destroy()
		# ...so we create a new one
		self.menu = gtk.Menu()
		# And finally connect tray icon to the menu
		self.handlerid = self.statusIcon.connect('activate', self.popup_menu_cb, self.menu)
